Welcome to JavaScript APIs. After watching this video, you will be able to explain how to work with nodes, describe how to modify the content of an element, explain how to modify the inline
style and attributes of an object, and describe how to use the Window
object methods and events. The DOM Level 1 core and Level 1 HTML specifications have too much detail for one unit.
Instead, the remainder of the unit focuses on script functions and properties that
are commonly used when working with HTML pages. Some of these common APIs
used in when accessing HTML DOM elements in web pages are seen on the slide, DOM elements are reviewed next. To retrieve a node reference for an
element of a document given an id, use the document.getElementById function and pass the id value as an argument. This returns one specific HTML or XML element if
a matching unique id is found on the web page. The getElementsByTagName function retrieves a NodeList of elements
with a specified tag name. The NodeList contains an array
of elements in your document. You provide the name of the
element you are interested in and an array of all elements with that
name found in the document is returned. For HTML elements, the tagName parameter
is the literal name of the HTML tag. If you run the function getElementsByTagName
with a “p” as a parameter argument, a NodeList of all the paragraphs
in the document is returned. This example shows how you can retrieve
all the image elements from a web page by using the getElementsByTagName function and
passing “img” as the parameter argument. The result is a NodeList that is
assigned to the variable imgSet. In the next part of the code, you loop through the nodeList and concatenate the
results into a DOM output stream. The src attribute is a common
property for the img tag. The src attribute indicates the
location of the image source. The document.write() function adds the
script-generated HTML to the document. You can use the DOM API function document.createElement(TagName) to create an element in the current document. After creating the element, you can
use any number of functions to place the element in the appropriate
location within the document. Examples of these functions include
the insertBefore, appendChild, or replaceChild function that can be used to
add the newly created element into the document. This example shows the source code
for adding a node to a document. A new paragraph element is being created that includes a text node with
the string “Hello world!” The text node is then appended as
a child of the paragraph element. Finally, the entire paragraph
with text is appended as a child node at the end of the body node of the HTML page. The function element.innerHTML retrieves or sets the contents of an HTML element. The innerHTML property returns all
child elements as a text string. With the element.innerHTML function, you
can change the contents of an HTML element, by setting it to a text string
that can include HTML tags. Setting the innerHTML value of an element to a
string removes all of the current child elements. The browser then parses the string and
sets the contents of the HTML element. You can use the element.style method to retrieve or set the inline CSS
style for a particular element. If you use element.style to
set the style of an element, it overrides any setting from a CSS
style sheet with one specific style. The way to set the style in JavaScript is with
the format element.style.propertyName = value. For example, if you had an
element div style="color:blue". Here the <div> tag is used to group
block-elements to format them with a color style. You can change the style of this div
tag with the JavaScript statement: div.style.color = 'red'; In contrast, the element.setAttribute('style',
…) wipes out all previously set inline CSS styles. The function element.setAttribute
with parameters(attrName, attrValue) dynamically modifies the attribute of an element. In the example, the src attribute of an element with an id of theImage is set
to a different target image. The function element.removeAttribute(attrName)
removes an attribute from an element. The function element.getAttribute(attrName) retrieves the value of the specified
attribute in the element, if it exists. Here are some window object functions and events. To open a new browser window,
use the window.open() function. This method returns a reference
to the new window object. You can use this reference
later to close the window, with the reference_name followed
by the close() function. The parameters of the window.open function are: URL - A string that indicates the location of
the web page to be displayed in the new window. You can pass an empty string
if you are going to write some script-generated content to the new
window in the current URL context. Name - A string that specifies
the name of the window. Features - An optional string that
specifies the features of the window, such as its placement and dimensions. The features string is a comma-separated
list of name-value pairs. Replace – An optional boolean value. If true, the new location replaces the
current page in the browser history. The window.onload function can be used to
start a function after the page is loaded. The function, window.dump("message") writes a
string into the console for the web browser. The dump() function is a
less intrusive way to display diagnostic information than the alert() method. Finally, the window.scrollTo(x-value, y-value) scrolls the web browser to a
particular set of coordinates on a page. The onload event handler runs in the current
window after the documents loads the web page. In the example, the onload event causes an
anonymous function to run. This function, in turn, runs the function addPara(). In this video, you learned you can retrieve a reference to a node using: document.getElementById(id) - Returns one
specific element that is based on the id attribute. document.getElementsByTagName(tagName) -
retrieves a of elements with the specified tag. You can create an element using: document.createElement(TagName) and place it using: insertBefore, appendChild, or replaceChild. You can modify elements using: element.innerHTML to retrieve or
set the contents of an HTML element, element.style to retrieve
or set the inline CSS style, element.setAttribute to modify
the attributes of an element. You can manage a window object
using functions including: window.open to return reference to a
new window object for the web browser, window.dump("message") to write a string
into the console for the web browser. This is not an exhaustive list
– many more functions exist that enable you to work with HTML elements and nodes.